package net.ecpt.crm.services;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.wvtools.utils.ExcelPoiUtils;

import javolution.util.FastList;
import javolution.util.FastMap;

public class CrmDayOrderQtyXlsServices {
    public static final String module = CrmDayOrderQtyXlsServices.class.getName();

    /**
     * 导入
     */
    public static Map<String, Object> importXls(DispatchContext dctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
//	Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
	Delegator delegator = dctx.getDelegator();
	 List<GenericValue> toBeStore = new ArrayList();
	List<String> messageList = new ArrayList<String>();
	List<Map<String, Object>> sheetList = (List<Map<String, Object>>) context.get("sheetList");
	LocalDispatcher dispatcher = dctx.getDispatcher();
	GenericValue userLogin = (GenericValue) context.get("userLogin");
	// String createBy = userLogin.getString("partyId");
	try {
	    // Map<String, Object> row0 =sheetList.get(0);
	    // String salaryCode1_dicText = ExcelPoiUtils.getCellString(row0, 2);
	    int i = 0;

	    for (Map<String, Object> row : sheetList) {
		i = i + 1;
		String warehouseId = ExcelPoiUtils.getCellString(row, 0);
		String customerId = ExcelPoiUtils.getCellString(row, 1);
		String warehouseName = ExcelPoiUtils.getCellString(row, 2);
		String customerName = ExcelPoiUtils.getCellString(row, 3);
		String orderQty = ExcelPoiUtils.getCellString(row, 4);
		String orderDate = ExcelPoiUtils.getCellString(row, 5);
		// 仓库代码,客户代码,仓库名称 客户名称 单数  出库日期
		
		// 判断
		if (UtilValidate.isEmpty(warehouseId)) {
		    messageList.add("第" + (i + 1) + "行,仓库代码不能为空。");
		    continue;
		}
		
		// 判断
		if (UtilValidate.isEmpty(customerId)) {
		    messageList.add("第" + (i + 1) + "行,客户代码不能为空。");
		    continue;
		}

		// 判断
		if (UtilValidate.isEmpty(orderDate)) {
		    messageList.add("第" + (i + 1) + "行,出库日期不能为空。");
		    continue;
		}
		
		// 创建
		GenericValue entity = delegator.makeValue("CrmDayOrderQty", UtilMisc.toMap("warehouseId", warehouseId,"customerId", customerId,"orderDate", orderDate));
					entity.set("warehouseName", warehouseName);
					entity.set("customerName", customerName);
					entity.set("orderQty", orderQty);
		toBeStore.add(entity);
						
						
		}
	    
	    delegator.storeAll(toBeStore);
	    
	    result.put("messageList", messageList);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	return result;
    }


    /**
     * 导出
     */

    public static Map<String, Object> exportXls(DispatchContext dctx, Map<String, ? extends Object> context) {
	Delegator delegator = dctx.getDelegator();
	String ids = (String) context.get("ids");
	List<String> baseIdList = FastList.newInstance();
	if (UtilValidate.isNotEmpty(ids)) {
	    baseIdList = Arrays.asList(ids.split(";"));
	}
	Map<String, Object> result = ServiceUtil.returnSuccess();

	EntityCondition mainCond = null;
	List<EntityCondition> andExprs = FastList.newInstance();
	if (UtilValidate.isNotEmpty(baseIdList))
	    andExprs.add(EntityCondition.makeCondition("warehouseId", EntityOperator.IN, baseIdList));
	if (andExprs.size() > 0)
	    mainCond = EntityCondition.makeCondition(andExprs, EntityOperator.AND);

	try {
	    List<GenericValue> entityList = delegator.findList("CrmDayOrderQty", mainCond, null, null, null, true);

	    List<Map<String, Object>> list = FastList.newInstance();
	    Map<String, Object> keyMap = FastMap.newInstance();
	    keyMap.put("0", "warehouseId");
	    keyMap.put("1", "customerId");
	    keyMap.put("2", "warehouseName");
	    keyMap.put("3", "customerName");
	    keyMap.put("4", "orderQty");
	    keyMap.put("5", "orderDate");
	    String headers = "仓库代码,客户代码,仓库名称,客户名称,单数,出库日期";
	    result.put("headers", headers);

	    for (GenericValue entity : entityList) {
		Map<String, Object> map = FastMap.newInstance();
		
		String warehouseId = entity.getString("warehouseId");
		String customerId = entity.getString("customerId");
		String warehouseName = entity.getString("warehouseName");
		String customerName = entity.getString("customerName");
		Long orderQty = entity.getLong("orderQty");
		Date orderDate = entity.getDate("orderDate");

		map.put("warehouseId", warehouseId);
		map.put("customerId", customerId);
		map.put("warehouseName", warehouseName);
		map.put("customerName", customerName);
		map.put("orderQty", orderQty);
		map.put("orderDate", orderDate);

		

		list.add(map);
	    }
	    result.put("list", list);
	    result.put("keyMap", keyMap);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	return result;
    }

}
